package de.jmda.mview.fx.node.typeshape.relation;

import java.util.Collection;
import java.util.LinkedList;

import javafx.scene.Node;

public abstract class Relation
{
	public enum Orthogonality { ON, OFF }
	
	/**
	 * constructor injection
	 */
	protected RelationEndpointSource source;

	/**
	 * constructor injection
	 */
	protected RelationEndpointTarget target;
	
	protected Orthogonality orthogonality = Orthogonality.OFF;

	/**
	 * lazy initialisation in {@link #getRelationSegments()}
	 */
	protected Collection<RelationSegment> relationSegments;

	public Relation(RelationEndpointSource source, RelationEndpointTarget target)
	{
		super();

		this.source = source;
		this.target = target;
	}

	public RelationEndpointSource getSource()
	{
		return source;
	}

	public RelationEndpointTarget getTarget()
	{
		return target;
	}

	/**
	 * override this default implementation to provide non direct point to point
	 * relation segments
	 *
	 * @return
	 */
	public Collection<RelationSegment> getRelationSegments()
	{
		if (relationSegments == null)
		{
			// lazy initialisation
			relationSegments = new LinkedList<RelationSegment>();

			// create segment, starting at source's and ending at target's tail
			relationSegments.add(
					new RelationSegmentStraight(
							source.arrow.tailProperty(), target.arrow.tailProperty()));
		}

		return relationSegments;
	}

	public Node[] getNodes()
	{
		Collection<Node> result = new LinkedList<Node>();

		result.add(source.getNode());

		for (RelationSegment relationSegment : getRelationSegments())
		{
			result.add(relationSegment);
		}

		result.add(target.getNode());

		return result.toArray(new Node[] {});
	}

	public String coordinatesAsString()
	{
		return
				"source tail " + source.arrow.getTail() + " " +
				"target tail " + target.arrow.getTail();
	}
}